//
//  XMCompareScanController.m
//  XiaoMiao
//
//  Created by 23 on 2018/3/31.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMCompareScanController.h"
#import "XMAlbumScrollView.h"

@interface XMCompareScanController ()


/**standard*/
@property(nonatomic,weak) XMAlbumScrollView *standardImageView;
@property(nonatomic,weak) XMAlbumScrollView *outImageView;

@end

@implementation XMCompareScanController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[SDImageCache sharedImageCache]clearMemory];

    //标准图
    XMAlbumScrollView *standardImageView = [[XMAlbumScrollView alloc] init];
    standardImageView.backgroundColor = [UIColor whiteColor];
    standardImageView.isCompare = YES;
    XMAlbumScanModel *imageModel = [[XMAlbumScanModel alloc] init];
    imageModel.imageUrl = self.standardImageUrl;
    UIImage *placeholderImg = nil;
    if (placeholderImg == nil) {
        
        
        placeholderImg = [UIImage imageNamed:@"ic_common_avatar_default"];
        
    }
    imageModel.image = placeholderImg;
    standardImageView.imageModel = imageModel;
    standardImageView.isStandard = YES;
    standardImageView.backgroundColor = kRGBColor(230, 230, 230);
    WEAKSELF
    standardImageView.callback = ^(CGFloat zoom, CGPoint offset) {
        weakSelf.outImageView.zoom = zoom;
    };
    standardImageView.offsetCallback = ^(CGPoint offset, XMAlbumScrollView *scrollView) {
        if (![weakSelf.outImageView isEqual:scrollView]) {
            weakSelf.outImageView.offset = offset;
        }
    };
    [self.view addSubview:standardImageView];
    self.standardImageView = standardImageView;
    
    
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.numberOfTouchesRequired  =1;
    [standardImageView addGestureRecognizer:singleTapGesture];
    

    
    //成像后的显示视图
    XMAlbumScanModel *imageModel1 = [[XMAlbumScanModel alloc] init];
    imageModel1.imageUrl = self.outImageUrl;
    UIImage *placeholderImg1 = nil;
    if (placeholderImg1 == nil) {
        placeholderImg1 = [UIImage imageNamed:@"ic_common_avatar_default"];
    }
    imageModel1.image = placeholderImg1;
    XMAlbumScrollView *outImageView = [[XMAlbumScrollView alloc] init];
    outImageView.isCompare = YES;
    outImageView.backgroundColor = kRGBColor(230, 230, 230);
    outImageView.layer.masksToBounds = YES;
    outImageView.isStandard = YES;
    outImageView.imageModel = imageModel1;
    outImageView.callback = ^(CGFloat zoom, CGPoint offset) {
        weakSelf.standardImageView.zoom = zoom;
    };
    
    outImageView.offsetCallback = ^(CGPoint offset, XMAlbumScrollView *scrollView) {
        if (![weakSelf.standardImageView isEqual:scrollView]) {
            weakSelf.standardImageView.offset = offset;
        }
    };
    [self.view addSubview:outImageView];
    self.outImageView = outImageView;
    
    
    UITapGestureRecognizer *singleTapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGesture1.numberOfTapsRequired = 1;
    singleTapGesture1.numberOfTouchesRequired  =1;
    [outImageView addGestureRecognizer:singleTapGesture1];
    
    
    [self.standardImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.5);
    }];
    

    [self.outImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(self.view);
        make.left.mas_equalTo(self.standardImageView.mas_right);
    }];
    
}


-(void)handleSingleTap:(UIGestureRecognizer *)sender{

    [self dismissViewControllerAnimated:NO completion:nil];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarHidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    
    //强制横屏
    self.view.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    //强制竖屏
    self.view.transform = CGAffineTransformIdentity;
}

-(void)dealloc{
    [[SDImageCache sharedImageCache]clearMemory];

}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache]clearMemory];

}

@end
